using System;
using IStrong.EC.Abstractions.Interfaces;
using IStrong.EC.DAO.Abstractions.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Linq;
using IStrong.EC.ExtensionMethods;
using IStrong.EC.WebApi.Results.Entity;
using System.Collections.Generic;
using NPOI.HSSF.UserModel;
using Newtonsoft.Json.Linq;
using NPOI.SS.UserModel;
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace IStrong.EC.WebApi.Results.Repository
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class Maintenance : IService
    {
        /// <summary>
        /// 数据库操作组件
        /// </summary>
        private readonly IDbContext _db;

        /// <summary>
        /// 配置信息
        /// </summary>
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 日志组件
        /// </summary>
        private readonly ILogger<Maintenance> _logger;
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="db"></param>
        /// <param name="configuration"></param>
        /// <param name="logger"></param>
        public Maintenance(IDbContext db, ILogger<Maintenance> logger, IConfiguration configuration)
        {
            _db = db;
            _logger = logger;
            _configuration = configuration;
        }

        #region 查询数据

        /// <summary>
        /// 获取服务合同列表信息
        /// </summary>
        public IEnumerable<MaintenanceContracts> GetContractsLists(object param)
        {
            return _db.Query<MaintenanceContracts>("Maintenance.s_contracts", param);
        }

        /// <summary>
        /// 根据服务合同号获取项目信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenanceContracts> GetContractsByContractId(object param)
        {
            return _db.Query<MaintenanceContracts>("Maintenance.s_contracts_by_contractid", param);
        }

        /// <summary>
        /// 根据服务合同号获取项目跟进记录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenanceRecords> GetRecordsByContractId(object param)
        {
            return _db.Query<MaintenanceRecords>("Maintenance.s_records_by_contractid", param);
        }

        /// <summary>
        /// 根据计划月份获取计划项目信息列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenanceContracts> GetContractsByPlanMonth(object param)
        {
            return _db.Query<MaintenanceContracts>("Maintenance.s_contracts_by_planmonth", param);
        }

        /// <summary>
        /// 根据计划月份获取计划完成情况数据(自签/协签)
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenancePlan2> GetPlan2ByPlanMonth(object param)
        {
            return _db.Query<MaintenancePlan2>("Maintenance.s_plan2_by_planmonth", param);
        }

        /// <summary>
        /// 根据计划月份获取计划完成情况数据(大区)
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenancePlan> GetPlanByPlanMonth(object param)
        {
            return _db.Query<MaintenancePlan>("Maintenance.s_plan_by_planmonth", param);
        }

        /// <summary>
        /// 根据计划年份获取计划完成情况数据(自签/协签)
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenancePlan2> GetPlan2ByPlanYear(object param)
        {
            return _db.Query<MaintenancePlan2>("Maintenance.s_plan2_by_planyear", param);
        }

        /// <summary>
        /// 根据计划年份获取计划完成情况数据(大区)
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenancePlan> GetPlanByPlanYear(object param)
        {
            return _db.Query<MaintenancePlan>("Maintenance.s_plan_by_planyear", param);
        }

        /// <summary>
        /// 获取逐月服务合同计划与完成情况
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenanceDistribution> GetDistributionByMonth(object param)
        {
            return _db.Query<MaintenanceDistribution>("Maintenance.s_distribution_by_month", param);
        }

        /// <summary>
        /// 获取服务合同分组统计数据源
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenanceStatsSuccess> GetStatsSuccess(object param)
        {
            return _db.Query<MaintenanceStatsSuccess>("Maintenance.s_stats_success", param);
        }

        /// <summary>
        /// 根据合同编号获取项目计划信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<MaintenancePlanProcess> GetPlanProcessById(object param)
        {
            return _db.Query<MaintenancePlanProcess>("Maintenance.s_plan_process_by_id", param);
        }

        #endregion 查询数据

        #region 添加数据

        /// <summary>
        /// 新增/修改服务合同项目信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddMaintenance(object param)
        {
            return _db.Execute("Maintenance.iu_contracts", param);
        }

        /// <summary>
        /// 新增服务合同跟进记录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddRecords(object param)
        {
            return _db.Execute("Maintenance.i_records", param);
        }

        /// <summary>
        /// 新增/修改月份计划记录信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddPlanProcess(object param)
        {
            return _db.Execute("Maintenance.iu_plan_process", param);
        }

        /// <summary>
        /// 新增/修改大区年目标
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddAreaPlanYear(object param)
        {
            return _db.Execute("Maintenance.iu_area_plan_year", param);
        }

        /// <summary>
        /// 新增/修改年目标计划
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddPlanYear(object param)
        {
            return _db.Execute("Maintenance.iu_plan_year", param);
        }

        #endregion 添加数据

        #region 删除数据

        /// <summary>
        /// 根据合同编号删除项目信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int DelMaintenance(object param)
        {
            return _db.Execute("Maintenance.d_contracts", param);
        }

        /// <summary>
        /// 根据根据记录ID删除跟进记录信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int DelRecords(object param)
        {
            return _db.Execute("Maintenance.d_records", param);
        }

        /// <summary>
        /// 根据记录ID删除合同计划信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int DelPlanProcess(object param)
        {
            return _db.Execute("Maintenance.d_plan_process", param);
        }
        #endregion 删除数据
    }
}